package xyz.scootaloo.oj.iq

import org.junit.Test
import xyz.scootaloo.console.app.support.Tester

/**
 * @author flutterdash@qq.com
 * @since 2021/4/16 13:28
 */
class M0106 {

    private fun compressString(S: String): String {
        val source = java.lang.StringBuilder(S).append(' ')
        val cache = StringBuilder()
        var current = source[0]
        var count = 0
        for (c in source) {
            if (c == current) {
                count++
            } else {
                cache.append(current).append(count)
                if (cache.length > S.length)
                    return S
                if (count > 1)
                    count = 1
                current = c
            }
        }

        return if (cache.length >= S.length)
            S
        else
            cache.toString()
    }

    @Test
    fun test0() {
        Tester.createTest(this::compressString)
            .addCase("aabcccccaaa", "a2b1c5a3")
            .addCase("abbccd", "abbccd")
            .addCase("bb", "bb")
            .testAndShow()
    }

}
